import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import(/* webpackChunkName:'login_home_welocme' */ '../views/login.vue')
const Home = () => import(/* webpackChunkName:'login_home_welocme' */ '../views/Home.vue')
const Welcome = () => import(/* webpackChunkName:'login_home_welocme' */ '../views/welcome.vue')
const Users = () => import(/* webpackChunkName:'Users_Right_Roles' */ '../views/user/users.vue')
const Right = () => import(/* webpackChunkName:'Users_Right_Roles' */ '../views/prower/rights.vue')
const Role = () => import(/* webpackChunkName:'Users_Right_Roles' */ '../views/prower/roles.vue')

const Cate = () => import(/* webpackChunkName:'Cate_params' */ '../views/goods/cate.vue')
const Params = () => import(/* webpackChunkName:'Cate_params' */ '../views/goods/params.vue')
const List = () => import(/* webpackChunkName:'List_Add' */ '../views/goods/list.vue')

const Add = () => import(/* webpackChunkName:'List_Add' */ '../views/goods/add.vue')

const Order = () => import(/* webpackChunkName:'Order_Report' */ '../views/order/order.vue')
const Report = () => import(/* webpackChunkName:'Order_Report' */ '../views/report/report.vue')
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Right
      },
      {
        path: '/roles',
        component: Role
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航首位
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()

  const token = window.sessionStorage.getItem('token')

  if (!token) return next('/login')
  next()
})

export default router
